package com.caigou.service.impl;

import com.caigou.bean.cms.entity.ChannelConfig;
import com.caigou.bean.cms.param.CommonParam;
import com.caigou.bean.exam.entity.Users;
import com.caigou.enums.ResponseEnum;
import com.caigou.mapper.cms.ArticleDictionaryMapper;
import com.caigou.mapper.cms.ChannelConfigMapper;
import com.caigou.mapper.cms.ChannelMapper;
import com.caigou.mapper.cms.RouterMapper;
import com.caigou.mapper.exam.ChinaMapper;
import com.caigou.pojo.*;
import com.caigou.service.CollectionService;
import com.caigou.service.CommonService;
import com.caigou.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class CommonServiceImpl implements CommonService {

    @Resource
    private ArticleDictionaryMapper articleDictionaryMapper;
    @Resource
    private RouterMapper routerMapper;
    @Resource
    private ChinaMapper chinaMapper;
    @Resource
    private ChannelMapper channelMapper;
    @Resource
    private ChannelConfigMapper channelConfigMapper;
    @Resource
    private CollectionService collectionService;

    @Override
    public Result<List<DictionaryList>> loadDictionaryList(CommonParam param) {
        List<DictionaryList> list = articleDictionaryMapper.loadDictionaryList(param);
        return Result.success(list);
    }

    @Override
    public Result<List<RouterTableColumn>> loadPageTableData(CommonParam param, Users users) {
        List<RouterTableColumn> tableColumn = routerMapper.loadColumnListByPage(param.getPageUrl(),param.getTableUrl());
        tableColumn.forEach(item -> {
            // 处理用户点击状态
            int result  = routerMapper.loadUserColumnExist(users.getUserid(), item.getId());
            if (result == 0){
                item.setClickStatus(1);// 如果用户没有存储过表格状态(默认点击状态设置为1显示)
            } else {
                item.setClickStatus(routerMapper.loadUserColumnClickStatus(users.getUserid(), item.getId()));
                item.setShowIndex(routerMapper.loadUserColumnShowIndex(users.getUserid(),item.getId()));
            }
        });
        Comparator<RouterTableColumn> bya = Comparator.comparing(RouterTableColumn::getShowIndex);//按照a升序
        Comparator<RouterTableColumn> byb = Comparator.comparing(RouterTableColumn::getId);//按照b升序
        Collections.sort(tableColumn,bya.thenComparing(byb));//将aList按照a字段先升序再按照B字段进行升序排列
        return Result.success(tableColumn);
    }

    @Override
    public Result saveUserPageTableData(List<CommonParam> params, Users users) {
        params.forEach(item ->{
            // 判断当前用户是否保存过当前字段的状态
            int result = routerMapper.loadUserColumnExist(users.getUserid(), item.getColumnId());
            if (result == 0){
                routerMapper.saveUserPageTableData(users.getUserid(),item.getColumnId(),item.getClickStatus(),item.getShowIndex());
            } else {
                routerMapper.updateUserPageTableData(users.getUserid(),item.getColumnId(),item.getClickStatus(),item.getShowIndex());
            }
        });
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result<List<DictionaryList>> loadChinaDictionaryList(CommonParam param) {
        List<DictionaryList> list = chinaMapper.loadChinaDictionaryList(param.getSuperId());
        return Result.success(list);
    }

    @Override
    public Result<List<TitleDictionaryList>> loadTitleDictionaryList(CommonParam param) {
        List<TitleDictionaryList> titleDictionaryList = articleDictionaryMapper.loadTitleDictionaryList(param);
        return Result.success(titleDictionaryList);
    }

    @Override
    public Result<List<ArticleAuditList>> loadArticleAuditList(CommonParam param, Users users) {
        List<ArticleAuditList> articleAuditList = routerMapper.loadArticleAuditList(users.getUserid());
        return Result.success(articleAuditList);
    }

    @Override
    public Result<List<LawLevelDictionaryList>> loadLawLevelDictionaryList() {
        List<LawLevelDictionaryList> list = articleDictionaryMapper.loadLawLevelDictionaryList();
        return Result.success(list);
    }

    @Override
    public Result<List<ArticleAuditList>> loadUserListByName(CommonParam param) {
        List<ArticleAuditList> list = routerMapper.loadUserListByName(param.getSelectName());
        return Result.success(list);
    }

    @Override
    public Result loadChannelList(CommonParam param) {
        List<DictionaryList> list = channelMapper.loadChannelList(param.getSuperId().toString());
        // 获取第二层
        list.forEach(item -> {
            item.setChildDictionaryList(channelMapper.loadChannelList(item.getValue()));
        });
        return Result.success(list);
    }

    @Override
    public Result loadSubstation() {
        List<DictionaryList> list = channelMapper.loadSubstation();
        return Result.success(list);
    }

    @Override
    public Result loadCollectionList() {
        List<DictionaryList> list = collectionService.loadCollectionListDictionary();
        return Result.success(list);
    }

    @Override
    public Result loadPublishChannelList() {
        List<DictionaryList> list = channelMapper.loadChannelList2("1");
        list.forEach(item1-> {
            // 获取第二层
            List<DictionaryList> list2 = channelMapper.loadChannelList(item1.getValue());
            list2.forEach(item2-> {
                // 获取第三层
                List<DictionaryList> list3 = channelMapper.loadChannelList(item2.getValue());
                list3.forEach(item3-> {
                    // 获取第四层
                    List<DictionaryList> list4 = channelMapper.loadChannelList(item3.getValue());
                    if (null != list4){
                        list4.forEach(item4 -> {
                            // 获取标题配置
                            int channelConfig = channelConfigMapper.loadTitleTypeConfig(item4.getValue());
                            item4.setTitleConfig(channelConfig);
                        });
                    }
                    item3.setChildDictionaryList(list4);
                });
                item2.setChildDictionaryList(list3);
            });
            item1.setChildDictionaryList(list2);
        });
        return Result.success(list);
    }
}
